//
//  HSScoreCell.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/10/10.
//  Copyright © 2016年 赖虹宇. All rights reserved.
//

import UIKit

class HSScoreCell: UITableViewCell {
    
   
    //科目成绩名
    var subjectScoreL = UILabel()
    //科目名
    var subjectsName = UIButton()
    //老师名
    var tName = UILabel()
    //发布时间
    var createDate = UILabel()
    //提交状态
    var scoreVc  = UIImageView()
    //成绩label
    var scoresL = UILabel()
    //合格label
    var qualified = UILabel()
    //老师评语视图
    var teacherEvaluation = UIView()
    //老师评语label
    var teaComment = UILabel()
    
    var scoreVM : HSScoreViewModel? {
        didSet{
            subjectScoreL.text = scoreVM?.scoreM.es_title
            subjectsName.setTitle(scoreVM?.scoreM.es_course, for: .normal)
            createDate.text = scoreVM?.scoreM.es_createDate
            tName.text = scoreVM?.scoreM.es_teacherName
            let scoreNum = scoreVM?.scoreM.es_score ?? 0
            scoresL.text = "\(scoreNum)"
            
            teaComment.attributedText = scoreVM?.teachercomments
            
            if (scoreVM?.scoreM.es_score)! >= 60 {
                scoreVc.image = UIImage(named: "scores_ok")
                qualified.text = "合格"
            }else{
                scoreVc.image = UIImage(named: "scores_ng")
                qualified.text = "不合格"
            }
            if scoreVM?.scoreM.isHiden == false {
                teacherEvaluation.isHidden = true
            }else{
            teacherEvaluation.isHidden = false
            }
            

        }
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
       setUpUI()
        
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //设置自定义cell的布局且约束
    func setUpUI(){
        //作业标题
        subjectScoreL = UILabel()
        subjectScoreL.textColor = UIColor().colorWithHex(0x4c4c4c)
        addSubview(subjectScoreL)
        subjectScoreL.snp.makeConstraints { (make) in
            make.top.equalTo(16.5)
            make.left.equalTo(16.5)
            make.width.equalTo(150)
            make.height.equalTo(30)
        }
        
        
        //科目--用textfield 方便设置圆角
        subjectsName.layer.cornerRadius = 10
        subjectsName.setTitleColor(UIColor().colorWithHex(0x97fd7ff), for: .normal)
        subjectsName.layer.borderColor = UIColor().colorWithHex(0x97fd7ff).cgColor
        subjectsName.layer.borderWidth = 1
        subjectsName.titleLabel?.font = UIFont.systemFont(ofSize: 11)
        
        //设置这个按钮不可交互
        subjectsName.isUserInteractionEnabled = false
        addSubview(subjectsName)
        subjectsName.snp.makeConstraints { (make) in
            make.left.equalTo(subjectScoreL.snp.left)
            make.top.equalTo(subjectScoreL.snp.bottom).offset(8)
            make.width.equalTo(44)
            make.height.equalTo(20)
        }
        //教师名
        tName.textColor = UIColor().colorWithHex(0x4c4c4c)
        tName.font = UIFont.systemFont(ofSize: 15)
        tName.textAlignment = .right
        addSubview(tName)
        tName.snp.makeConstraints { (make) in
            make.top.equalTo(16.5)
            make.left.equalTo(subjectScoreL.snp.right).offset(40)
            make.width.equalTo(60)
            make.height.equalTo(25)
        }
        //发布时间
        createDate.textColor = UIColor().colorWithHex(0x999999)
        createDate.textAlignment = .center
        createDate.font = UIFont.systemFont(ofSize: 15)
        addSubview(createDate)
        createDate.snp.makeConstraints { (make) in
            make.top.equalTo(tName.snp.bottom).offset(16.5)
            make.right.equalTo(tName.snp.right)
            make.bottom.equalTo(subjectsName.snp.bottom)
        }
        
        //成绩label
        scoresL.font = UIFont.systemFont(ofSize: 17)
        scoresL.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        scoreVc.addSubview(scoresL)
        //合格label
        qualified.font = UIFont.systemFont(ofSize: 12)
        qualified.textColor = #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1)
        scoreVc.addSubview(qualified)
        
        //设置约束
        scoreVc.layer.cornerRadius = 30
        addSubview(scoreVc)
        scoreVc.snp.makeConstraints { (make) in
            make.top.equalTo(tName.snp.top)
            make.left.equalTo(tName.snp.right).offset(20)
            make.width.height.equalTo(60)
        }
        scoresL.snp.makeConstraints { (make) in
            make.top.equalTo(12)
            make.left.equalTo(18)
            make.right.equalTo(16)
            make.height.equalTo(20)
        }
        qualified.snp.makeConstraints { (make) in
            make.top.equalTo(scoresL.snp.bottom).offset(5)
            make.left.equalTo(scoresL.snp.left).offset(-6)
            
        }
        teacherEvaluation.backgroundColor = UIColor().colorWithHex(0xf0f0f0)
        teacherEvaluation.isHidden = true
        addSubview(teacherEvaluation)
        teacherEvaluation.snp.makeConstraints { (make) in
            make.top.equalTo(scoreVc.snp.bottom).offset(8)
            make.left.equalTo(subjectsName.snp.left)
            make.right.equalTo(-16)
            make.height.equalTo(30)
        }
        //将老师评语label添加到老师评语视图
        teacherEvaluation.addSubview(teaComment)
        teaComment.snp.makeConstraints { (make) in
            make.top.equalTo(8)
            make.left.equalTo(16)
            
        }
    }
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}
